package com.company.jncz.web.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CommonController {
	
	private static final String REDIRECT_TO_TOPIC_LIST = "redirect:topic/list.do";
	@RequestMapping("/success")
	public ModelAndView success(@RequestParam String path){
		ModelAndView mav = new ModelAndView();
		mav.addObject("path", path);
		mav.setViewName("success");
		return mav;
	}
	
	@RequestMapping("/login")
	public String login(@RequestParam String username,@RequestParam String password,HttpServletRequest req){
		HttpSession session = req.getSession();
		session.setAttribute("user", "user");
		return REDIRECT_TO_TOPIC_LIST;
	}
	@RequestMapping("/logout")
	public String logout(HttpServletRequest req){
		HttpSession session = req.getSession(false);
		if(session!=null)
			session.invalidate();
		return REDIRECT_TO_TOPIC_LIST;
	}
}
